<% if !mobile_device? && !@grostitre.nil?%>
<h1 class="mobile-content_une"><%=@match_une.tournament.name%></h1>
<% end %>

<div class="main_content_4_8">
  <div class="main_left">
    <% if @matches.count != 0 %>

      <% if !mobile_device? %>
        <% if @matches.count != 0 %>
          <div class="grostitre">
            <% if !@grostitre.nil? %>
              <%= @grostitre %>
            <% else  %>
              <%= @team_grostitre_denominateur.nil? ? "" : @team_grostitre_denominateur %> <%= @team_grostitre %> l'emporte
            <% end %>
          </div>
        <% else %>
          <div class="grostitre">
            Pas de match rencontre
          </div>
        <% end %>
        <% if @matches.count != 0 %>
        <div class="title_w_bg title_une">A la une
          <div class="title_tournament"><%= @match_une.tournament.name %></div>
          <div class="title_journee"><%= @match_une.tournament.matches.count %>e journée</div>
        </div>
        <% end %>
      <% end %>

      <div class="image_une">
        <%= link_to image_tag(@image_a_la_une, size: "700x416", alt: "Illustration"), match_path(@match_une)%>
      </div>
      <div class="score_une">
          <tr>
            <div class="mobile-player_une">
              <% if mobile_device? %><!-- Afficher d'abord l'equipe sur mobile -->
                <td><%= @match_une.teams.first.club.name %> </td>
              <% end %>
              <td><span class="tab_player">
                <%= @match_une.teams.first.player.pseudo %> </td>
              </span></td>
              <% if !mobile_device? %><!-- Afficher l'equipe après le joueur sur ordi -->
                <td><%= @match_une.teams.first.club.name %> </td>
              <% end %>
            </div>
            <td>
              <div class="tab_score_une">
                  <div class="tab_score_one <%= 'winner' if @match_une.teams.first.status == 'winner' %>">
                    <%= @match_une.teams.first.score %>
                  </div>
                  <div class="tab_score_two <%= 'winner' if @match_une.teams.last.status == 'winner' %>">
                    <%= @match_une.teams.last.score %>
                  </div>
              </div>
            </td>
            <div class="mobile-player_une">
              <td><%= @match_une.teams.last.club.name %></td>
              <td><span class="tab_player">
                  <%= @match_une.teams.last.player.pseudo %>
                  </span>
              </td>
            </div>

          </tr>
      </div>
      <% if mobile_device? %>
        <div class="grostitre_une">
          <% if mobile_device? %>
            A la une | <%= @match_une.tournament.matches.count %>e journée
          <% end %>
          <div class="grostitre_mobile">
            <% if !@grostitre.nil? %>
              <%= @grostitre %>
            <% else  %>
              <%= @team_grostitre_denominateur.nil? ? "" : @team_grostitre_denominateur %> <%= @team_grostitre %> l'emporte
            <% end %>
          </div>
        </div>
      <% end %>

    <% end %>

    <br>
    <% if !@match_une.nil? %>
        <div class="match_une_links mobile-content_une"> <%= link_to ">  Voir la compétition", tournament_path(@match_une.tournament) %>
          <%= link_to ">  Le résumé du match", match_path(@match_une) %>
        </div>
    <% end %>
    <br>

    <div class="title_w_bg">Les 5 derniers matchs</div>

    <table class="scoreboard">

      <% if @matches.count == 0 %>
        <tr> <td> Pas de match enregistré </td> </tr>
      <% elsif @match_une.tournament.matches.count > 1 %>
              <tr>
                <th>Mode</th>
                <th>J1</th>
                <th>Team 1</th>
                <th>Score</th>
                <th>Team 2</th>
                <th>J2</th>
              </tr>
              <br>

              <% @match_une.tournament.matches.order(id: :desc).first(5).each do |match| %>
                <% if match.teams.empty? %>
                <tr>
                  <td><%= match.id %></td>
                  <td>....</td>
                  <td><%= link_to 'Supprimer', match_path(match), method: :delete,
                          data: { confirm: 'Etes vous sur ?' }%></td>

                </tr>
                <% else %>
                <tr>
                  <td><%= match.mode %></td>
                  <td><span class="tab_player">
                    <%= link_to Player.find(match.teams.first.player_id).pseudo, player_path(match.teams.first.player_id) %> </td>
                  </span></td>

                  <% if match.teams.first.status == "winner" %><td class="statuswinner">
                    <% else %><td>
                  <% end %>
                  <%= match.teams.first.club.name %> </td>
                  <td><div class="tab_score">
                        <%= match.teams.first.score %> - <%= match.teams.last.score %>
                      </div>
                      <span class="tab_score_prol">
                      <%= (match.prolongations && !match.teams.first.prol_score) ? "a.p" : "" %>
                      <%= match.teams.first.prol_score %><%= match.teams.first.prol_score == nil ? '' : ' tab ' %><%= match.teams.last.prol_score %>
                      </span>
                  </td>
                  <% if match.teams.last.status == "winner" %><td class="statuswinner">
                    <% else %><td>
                  <% end %>
                  <%= match.teams.last.club.name %></td>
                  <td><span class="tab_player">
                      <%= link_to Player.find(match.teams.last.player_id).pseudo, player_path(match.teams.last.player_id) %>
                      </span>
                  </td>

                  <td><%= link_to "Détails", match_path(match)%></td>
                  <% if user_signed_in? %><td><%= link_to "Modifier", edit_match_path(match)%></td><% end %>
                 </tr>
                 <% end %>
              <% end %>
      <% else %>
      <br>Pour l'instant, cette compétition n'a pas d'autre match.
      <% end %>
    </table>

    <br>

    <div class="title_w_bg">Classement </div>
    <% if @matches.count != 0 %>

    <table class="leaderboard mobile-content_une">
      <tr>
        <th> </th>
        <th> </th>
        <th>Pts</th>
        <th>J.</th>
        <th>G.</th>
        <th>G.pr</th>
        <th>G.pn.</th>
        <th>N.</th>
        <th>D.</th>
        <th>D.pr</th>
        <th>D.pn.</th>
        <th class="lb_br">Diff.</th>
      </tr>
      <br>

      <% i = 0
        @match_une.tournament.seasons.order(points: :desc).each do |season|
        i = i+1.to_i
      %>
        <tr>
          <td><%= i %></td>
          <td class="lead_team"><%= link_to season.player.pseudo, player_path(season.player) %></td>
          <td><b><%= season.points %></b></td>
          <td class="lb_br"><%= season.teams.count %> </td>
          <td><%= season.win %> </td>
          <td><%= season.win_prol %> </td>
          <td class="lb_br"><%= season.win_peno %> </td>
          <td class="lb_br"><%= season.draw %> </td>
          <td><%= season.lose %> </td>
          <td><%= season.lose_prol %> </td>
          <td class="lb_br"><%= season.lose_peno %> </td>
          <td class="lb_br"><%= (Team.all.where("season_id= '#{season.id}'").sum(:score))-((Team.where("season_id= '#{season.id}'").collect{|a| a.match.teams.collect{|b| b.score}.sum }.sum)-(Team.all.where("season_id= '#{season.id}'").sum(:score))) %>
        </tr>
      <% end %>
    <% end %>

    </table>
    <br>
    <div class="title_w_bg">Reglement en vigueur </div>
    <% if @matches.count != 0 %>
    <ul class="list_rules mobile-content_une">
      <li>Match gagné : +<%= @match_une.tournament.win_regular_points %>
      <li>Match gagné après prolongations : +<%= @match_une.tournament.win_prol_points %>
      <li>Match gagné aux penalties : +<%= @match_une.tournament.win_peno_points %>
      <li><% if (@match_une.tournament.lose_regular_points && @match_une.tournament.lose_prol_points && @match_une.tournament.lose_peno_points) == 0 %>
          Pas de point pour le perdant.
        <% end %>
      <li>Match nul : +<%= @match_une.tournament.draw_regular_points %>

    </ul>
    <% end %>

  </div>

  <div class="main_right">
    <div class="title_w_bg">Le vestiaire </div>
    <br>

    <table class="commentboard mobile-content_une">
      <% if @comments.count == 0 %>
        <div class="mobile-content_une">
          Pas de commentaire
        </div>
      <% else %>
        <% @comments.order("created_at DESC").each do |comment| %>
          <tr>
            <td class="comment_time"><%= comment.created_at.strftime("%H:%M") %></td>
            <td class="comment_content"><span class="comment_user"><%= comment.user.username %></span> : <%= comment.content %> </td>
          </tr>
        <% end %>
      <% end %>
    </table>
    <br>
    <div class="new_comment mobile-content_une">
      <% if user_signed_in? %>
          <%= form_for(Comment.new) do |com| %>
            <div class="field">
              <%= com.text_field :content, placeholder: "Commentaire" %>
            </div>
            <div class="actions">
              <%= com.submit "Envoyer" %>
            </div>
          <% end %>
      <% else %>
          <i>Vous devez être connecté pour pouvoir commenter.</i>
      <% end %>
    </div>
  </div>

</div>

<script type="text/javascript">
console.log(isMobileDevice());
</script>
